// app.js
const TronWeb = require('tronweb');
const TronGrid = require('trongrid');
const Web3 = require('web3');

class AppBootHook {
  constructor(app) {
    this.app = app;
  }

  configWillLoad() {
    console.log('configWillLoad-配置即将加载');
  }

  async didLoad() {
    console.log('didLoad-文件加载完成');
  }

  async willReady() {
    console.log('willReady-插件启动完毕');
    // 创建上下文对象
  }

  async didReady() {
    console.log('didReady-worker准备就绪');
  }

  async serverDidReady() {
    // 文件资源已经全部加载好了, 项目也已经启动成功
    console.log('serverDidReady-应用启动完成');
    this.initApp();
  }

  // 初始化web3对象和合约对象
  async initApp() {
    // 创建以太坊共用对象
    await this.initEth();
  }

  // 以太坊共用对象
  // 当前进度: 已使用最新的合约, 等待从新部署到服务器;
  async initEth() {
    const { app } = this;
    const isMian = true; // 主网还是测试网; false=测试网, true=主网 ============

    app.ETH.Web3 = new Web3(app.ETH.Url);

    if(isMian) {
      const HttpProvider = TronWeb.providers.HttpProvider;
      const fullNode = new HttpProvider(app.BSCMAIN.Url);
      const solidityNode = new HttpProvider(app.BSCMAIN.Url);
      const eventServer = new HttpProvider(app.BSCMAIN.Url);
      const privateKey = app.BSCMAIN.OwnerPrivateKey;
      app.BSCMAIN.Web3 = await new TronWeb(fullNode,solidityNode,eventServer,privateKey);
      app.BSCMAIN.TronGrid = new TronGrid(app.BSCMAIN.Web3);

      app.BSCMAIN.HnftContract = await app.BSCMAIN.Web3.contract(app.BSCMAIN.HnftAbi, app.BSCMAIN.HnftAddress);
      app.BSCMAIN.NftContract = await app.BSCMAIN.Web3.contract(app.BSCMAIN.NftAbi, app.BSCMAIN.NftAddress);
      app.BSCMAIN.NftMiningContract = await app.BSCMAIN.Web3.contract(app.BSCMAIN.NftMiningAbi, app.BSCMAIN.NftMiningAddress);
      app.BSCMAIN.RecycleContract = await app.BSCMAIN.Web3.contract(app.BSCMAIN.RecycleAbi, app.BSCMAIN.RecycleAddress);
      app.BSCMAIN.Mining2Contract = await app.BSCMAIN.Web3.contract(app.BSCMAIN.Mining2Abi, app.BSCMAIN.Mining2Address);
      app.Init.factoryContract = await app.BSCMAIN.Web3.contract(app.Init.factoryAbi, app.Init.factoryAddress);
      app.BSC = app.BSCMAIN;
    }else {
      const HttpProvider = await TronWeb.providers.HttpProvider;
      const fullNode = await new HttpProvider(app.BSCTEST.Url);
      const solidityNode = await new HttpProvider(app.BSCTEST.Url);
      const eventServer = await new HttpProvider(app.BSCTEST.Url);
      const privateKey = app.BSCTEST.OwnerPrivateKey;
      app.BSCTEST.Web3 = await new TronWeb(fullNode,solidityNode,eventServer,privateKey);
      app.BSCTEST.TronGrid = new TronGrid(app.BSCTEST.Web3);

      app.BSCTEST.HnftContract = await app.BSCTEST.Web3.contract(app.BSCTEST.HnftAbi, app.BSCTEST.HnftAddress);
      app.BSCTEST.NftContract = await app.BSCTEST.Web3.contract(app.BSCTEST.NftAbi, app.BSCTEST.NftAddress);
      app.BSCTEST.NftMiningContract = await app.BSCTEST.Web3.contract(app.BSCTEST.NftMiningAbi, app.BSCTEST.NftMiningAddress);
      app.BSCTEST.RecycleContract = await app.BSCTEST.Web3.contract(app.BSCTEST.RecycleAbi, app.BSCTEST.RecycleAddress);
      app.BSCTEST.Mining2Contract = await app.BSCTEST.Web3.contract(app.BSCTEST.Mining2Abi, app.BSCTEST.Mining2Address);
      // app.Init.factoryContract = await app.BSCMAIN.Web3.contract(app.Init.factoryAbi, app.Init.factoryAddress);
      app.BSC = app.BSCTEST;
    }

  }




}

module.exports = AppBootHook;
